﻿'use strict';

/**
 * Config for the router
 */
angular.module('app')
  .run(
    ['$rootScope', '$state', '$stateParams',
        function ($rootScope, $state, $stateParams) {
            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;
        }
    ]
  )

  .config(
    ['$stateProvider', '$urlRouterProvider', '$httpProvider', 'menuServiceProvider',
function ($stateProvider, $urlRouterProvider, $httpProvider, menuServiceProvider) {
    //$urlRouterProvider
    //          .otherwise("/access/signin");
    $stateProvider.state('app', {
        abstract: true,
        url: '/app',
        templateUrl: 'template/app.html',
        resolve: {
            deps: ['$ocLazyLoad',
              function ($ocLazyLoad) {
                  return $ocLazyLoad.load(['js/global.js', 'js/controllers/controllers.js']);
              }
            ]
        }
    })
    .state('access', {
        url: '/access',
        template: '<div ui-view class="fade-in-right-big smooth"></div>'
    })
    .state('access.signin', {
        url: '/signin',
        templateUrl: 'template/page_signin.html',
        resolve: {
            deps: ['uiLoad',
              function (uiLoad) {
                  return uiLoad.load(['js/controllers/signin.js']);
              }
            ] 
        }
    })
    
    .state('access.signup', {
        url: '/signup',
        templateUrl: 'template/page_signup.html',
        resolve: {
            deps: ['uiLoad',
              function (uiLoad) {
                  return uiLoad.load(['js/controllers/signup.js']);
              }
            ]
        }
    })
    .state('access.forgotpwd', {
        url: '/forgotpwd',
        templateUrl: 'template/page_forgotpwd.html',
    })
    if (!menuServiceProvider) {
        return;
    }    var menuService = menuServiceProvider.$get("menuService");
    for (var i = 0; i < menuService.menus.length; i++) {
        var menu = menuService.menus[i];
        if (menu.level == 1) {
            continue;
        }

        var strkey = menu.key;
        var reqiure = menu.reqiure;
        var urlstr = menuService.GetUrlString(strkey);
        var templateurlstr = menuService.GetTemplateUrlString(strkey);

        if (menu.isdefaultPath) {
            console.log(menu.isdefaultPath)
            var defaulturlstr = menuService.GetUrlString(strkey, menu.isdefaultPath);
            $urlRouterProvider
              .otherwise(defaulturlstr);
        }

        $stateProvider.state(strkey, {
            url: urlstr,
            params: menu.params,
            templateUrl: templateurlstr,

        });
    }

    $httpProvider.interceptors.push('httpLoadingMarker');
}])
.factory('httpLoadingMarker', ["$rootScope", function ($rootScope) {
    var httpLoadingMarker = {
        request: function (config) {
            if (config.url.indexOf(serverURL) >= 0) {
                $rootScope.loading = true;
            }
            return config;
        },
        response: function (response) {
            if (response.config.url.indexOf(serverURL) >= 0) {
                $rootScope.loading = false;
            }
            return response;
        }
    };
    return httpLoadingMarker;
}]);;